package com.airn.siege.lion.custom.widget

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.NumberPicker
import android.widget.Toast
import com.airn.siege.lion.custom.widget.databinding.DialogDateSelectorYmdhmsCNPDBinding
import com.airn.siege.lion.custom.widget.databinding.DialogDateSelectorYmdhmsChangeVerSiegeLionBinding
import com.airn.siege.lion.custom.widget.databinding.DialogDateSelectorYmdhmsSiegeLionBinding
import com.airn.siege.lion.lib.widget.number.picker.d.ChargeNumberPicker
import java.lang.reflect.Field
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*


/**
 * Create by Airn on 2022-08-18.
 */
object DateSelectorUtils {


    fun dateChangeCNPDSelector(
        activity: Activity, defaultTimeStr: String? = null, minDate: Date? = null, maxDate: Date? = null, showSplit: Boolean = false, success: (String, Date) -> Unit
    ) {
        val splitDate = "-" //日期分隔符
        val splitTime = ":" //时间分隔符
        val binding = DialogDateSelectorYmdhmsCNPDBinding.inflate(LayoutInflater.from(activity))

        if (!showSplit) {
            binding.tvYearSplit.visibility = View.GONE
            binding.tvMonthSplit.visibility = View.GONE
            binding.tvDaySplit.visibility = View.GONE

            binding.tvHourSplit.visibility = View.GONE
            binding.tvMinuteSplit.visibility = View.GONE
            binding.tvSecondSplit.visibility = View.GONE
        }

        val calendar = Calendar.getInstance()
        try {
            if (null != defaultTimeStr) { //"yyyy/MM/dd HH:mm:ss"
                val mSimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                val date = mSimpleDateFormat.parse(defaultTimeStr)
                if (null != date) calendar.time = date
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        var calendarMin: Calendar? = null
        try {
            if (null != minDate) {
                calendarMin = Calendar.getInstance()
                calendarMin.time = minDate
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        var calendarMax: Calendar? = null
        try {
            if (null != maxDate) {
                calendarMax = Calendar.getInstance()
                calendarMax.time = maxDate
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        binding.npYear.setDataCFormatter(binding.npYear)
        binding.npMonth.setDataCFormatter(binding.npMonth)
        binding.npDay.setDataCFormatter(binding.npDay)
        binding.npHour.setDataCFormatter(binding.npHour)
        binding.npMinute.setDataCFormatter(binding.npMinute)
        binding.npSecond.setDataCFormatter(binding.npSecond)

        binding.npYear.value = calendar[Calendar.YEAR]
        binding.npMonth.value = calendar[Calendar.MONTH] + 1

        binding.npDay.minValue = 1
        binding.npDay.maxValue = 31
        binding.npDay.value = calendar[Calendar.DAY_OF_MONTH]

        binding.npHour.minValue = 0
        binding.npHour.maxValue = 23
        binding.npHour.value = calendar[Calendar.HOUR_OF_DAY]

        binding.npMinute.minValue = 0
        binding.npMinute.maxValue = 59
        binding.npMinute.value = calendar[Calendar.MINUTE]

        binding.npSecond.minValue = 0
        binding.npSecond.maxValue = 59
        binding.npSecond.value = calendar[Calendar.SECOND]

        if (null != calendarMin && null != calendarMax) {

            binding.npYear.minValue = calendarMin[Calendar.YEAR]
            binding.npYear.maxValue = calendarMax[Calendar.YEAR]

            binding.npMonth.minValue = calendarMin[Calendar.MONTH] + 1
            binding.npMonth.maxValue = calendarMax[Calendar.MONTH] + 1

            if (calendarMin[Calendar.YEAR] == calendarMax[Calendar.YEAR]) { //不同年才需要设置年监听
                binding.npYear.setOnValueChangedListener { _, _, yearValue ->
                    val monthValue = binding.npMonth.value
                    val maxDay = getMaxDay(yearValue, monthValue)
                    if (calendarMin[Calendar.MONTH] + 1 == monthValue) { //最小月
                        binding.npDay.minValue = calendarMin[Calendar.DAY_OF_MONTH] //从最小月的日开始
                        binding.npDay.maxValue = maxDay //到最后一天结束
                    } else { //最大月
                        binding.npDay.minValue = 1 //从第一天开始
                        binding.npDay.maxValue = calendarMax[Calendar.DAY_OF_MONTH] //到设置的最大月的日
                    }
                }
            }

            if (calendarMin[Calendar.MONTH] == calendarMax[Calendar.MONTH]) { //同月
                binding.npDay.minValue = calendarMin[Calendar.DAY_OF_MONTH]
                binding.npDay.maxValue = calendarMax[Calendar.DAY_OF_MONTH]

                binding.npMonth.setOnValueChangedListener { _, _, _ ->
                    binding.npDay.minValue = calendarMin[Calendar.DAY_OF_MONTH]
                    binding.npDay.maxValue = calendarMax[Calendar.DAY_OF_MONTH]
                }
            } else { //不同月
                binding.npDay.minValue = calendarMin[Calendar.DAY_OF_MONTH]
                binding.npDay.maxValue = getMaxDay(calendarMin[Calendar.YEAR], calendarMin[Calendar.MONTH] + 1)

                binding.npMonth.setOnValueChangedListener { _, _, monthValue ->
                    val yearValue = binding.npYear.value
                    if (calendarMin[Calendar.MONTH] + 1 == monthValue) { //最小月
                        val maxDay = getMaxDay(yearValue, monthValue)
                        binding.npDay.minValue = calendarMin[Calendar.DAY_OF_MONTH] //从最小月的日开始
                        binding.npDay.maxValue = maxDay //到最后一天结束
                    } else { //最大月
                        binding.npDay.minValue = 1 //从第一天开始
                        binding.npDay.maxValue = calendarMax[Calendar.DAY_OF_MONTH] //到设置的最大月的日
                    }
                }
            }

            if (calendarMin[Calendar.MONTH] == calendarMax[Calendar.MONTH] && //同月
                calendarMin[Calendar.DAY_OF_MONTH] == calendarMax[Calendar.DAY_OF_MONTH] //同天
            ) { //默认
                binding.npHour.minValue = calendarMin[Calendar.HOUR_OF_DAY]
                binding.npHour.maxValue = calendarMax[Calendar.HOUR_OF_DAY]
            }

            if (calendarMin[Calendar.MONTH] == calendarMax[Calendar.MONTH] && //同月
                calendarMin[Calendar.DAY_OF_MONTH] == calendarMax[Calendar.DAY_OF_MONTH] && //同天
                calendarMin[Calendar.HOUR_OF_DAY] == calendarMax[Calendar.HOUR_OF_DAY] //同时
            ) { //默认
                binding.npMinute.minValue = calendarMin[Calendar.MINUTE]
                binding.npMinute.maxValue = calendarMax[Calendar.MINUTE]
            }

            if (calendarMin[Calendar.MONTH] == calendarMax[Calendar.MONTH] && //同月
                calendarMin[Calendar.DAY_OF_MONTH] == calendarMax[Calendar.DAY_OF_MONTH] && //同天
                calendarMin[Calendar.HOUR_OF_DAY] == calendarMax[Calendar.HOUR_OF_DAY] && //同时
                calendarMin[Calendar.MINUTE] == calendarMax[Calendar.MINUTE] //同分
            ) { //默认
                binding.npSecond.minValue = calendarMin[Calendar.SECOND]
                binding.npSecond.maxValue = calendarMax[Calendar.SECOND]
            }

            //日监听
            binding.npDay.setOnValueChangedListener { _, _, dayValue ->
                val yearValue = binding.npYear.value
                val monthValue = binding.npMonth.value
                if (calendarMax[Calendar.YEAR] == yearValue && //
                    calendarMax[Calendar.MONTH] + 1 == monthValue && //
                    calendarMax[Calendar.DAY_OF_MONTH] == dayValue //
                ) {
                    binding.npHour.maxValue = calendarMax[Calendar.HOUR_OF_DAY]
                }

                if (calendarMin[Calendar.YEAR] == yearValue && //
                    calendarMin[Calendar.MONTH] == monthValue && //
                    calendarMin[Calendar.DAY_OF_MONTH] == dayValue //
                ) {
                    binding.npHour.minValue = calendarMin[Calendar.HOUR_OF_DAY]
                }
            }

            //时监听
            binding.npHour.setOnValueChangedListener { _, _, hourValue ->
                val yearValue = binding.npYear.value
                val monthValue = binding.npMonth.value
                val dayValue = binding.npDay.value //日期为最大时
                if (calendarMax[Calendar.YEAR] == yearValue && //
                    calendarMax[Calendar.MONTH] + 1 == monthValue && //
                    calendarMax[Calendar.DAY_OF_MONTH] == dayValue && //
                    calendarMax[Calendar.HOUR_OF_DAY] == hourValue //
                ) {
                    binding.npMinute.maxValue = calendarMax[Calendar.MINUTE]
                }

                //时为最小时
                if (calendarMin[Calendar.YEAR] == yearValue && //
                    calendarMin[Calendar.MONTH] + 1 == monthValue && //
                    calendarMin[Calendar.DAY_OF_MONTH] == dayValue && //
                    calendarMin[Calendar.HOUR_OF_DAY] == hourValue //
                ) {
                    binding.npMinute.minValue = calendarMin[Calendar.MINUTE]
                }
            }

            //分监听
            binding.npMinute.setOnValueChangedListener { _, _, minuteValue ->
                val yearValue = binding.npYear.value
                val monthValue = binding.npMonth.value
                val dayValue = binding.npDay.value
                val hourValue = binding.npHour.value

                //时为最大时
                if (calendarMax[Calendar.YEAR] == yearValue && //
                    calendarMax[Calendar.MONTH] + 1 == monthValue && //
                    calendarMax[Calendar.DAY_OF_MONTH] == dayValue && //
                    calendarMax[Calendar.HOUR_OF_DAY] == hourValue && //
                    calendarMax[Calendar.MINUTE] == minuteValue //
                ) {
                    binding.npSecond.maxValue = calendarMax[Calendar.SECOND]
                }

                //时为最小时
                if (calendarMin[Calendar.YEAR] == yearValue && //
                    calendarMin[Calendar.MONTH] + 1 == monthValue && //
                    calendarMin[Calendar.DAY_OF_MONTH] == dayValue && //
                    calendarMin[Calendar.HOUR_OF_DAY] == hourValue && //
                    calendarMin[Calendar.MINUTE] == minuteValue //
                ) {
                    binding.npSecond.minValue = calendarMin[Calendar.SECOND]
                }

            }
        } else {
            binding.npYear.minValue = 1970
            binding.npYear.maxValue = 2099

            binding.npMonth.minValue = 1
            binding.npMonth.maxValue = 12

            binding.npDay.minValue = 1
            binding.npDay.maxValue = 31

            binding.npYear.setOnValueChangedListener { _, _, yearValue ->
                val monthValue = binding.npMonth.value
                val maxDay = getMaxDay(yearValue, monthValue)
                binding.npDay.minValue = 1
                binding.npDay.maxValue = maxDay
            }

            binding.npMonth.setOnValueChangedListener { _, _, monthValue ->
                val yearValue = binding.npYear.value
                val maxDay = getMaxDay(yearValue, monthValue)
                binding.npDay.minValue = 1
                binding.npDay.maxValue = maxDay
            }
        }

        //npd.setSelectionDivider(new ColorDrawable(Color.YELLOW));
        binding.npYear.wrapSelectorWheel = true // 是否循环显示内容

        AlertDialog.Builder(activity).setTitle("时间选择").setView(binding.root).setNegativeButton("取消") { dialog, _ ->
            dialog.dismiss()
        }.setPositiveButton("确定") { dialog, _ ->
            try {
                val ymd = (binding.npYear.getDataCFormater(binding.npYear)) + //
                        splitDate + (binding.npMonth.getDataCFormater(binding.npMonth)) + //
                        splitDate + (binding.npDay.getDataCFormater(binding.npDay)) //

                val hms = (binding.npHour.getDataCFormater(binding.npHour)) + //
                        splitTime + (binding.npMinute.getDataCFormater(binding.npMinute)) + //
                        splitTime + (binding.npSecond.getDataCFormater(binding.npSecond)) //
                val timeStr = "$ymd $hms"

                val dateByFormat = getDateByFormat(timeStr, "yyyy-MM-dd HH:mm:ss")
                if(null==dateByFormat){
                    Toast.makeText(activity,"时间格式不正确，请联系管理员",Toast.LENGTH_SHORT).show()
                    return@setPositiveButton
                }
                if(null!=calendarMin && null!=calendarMax){
                    Log.e("selectTime","time: "+dateByFormat.time)
                    Log.e("selectTime","calendarMax: "+calendarMax.timeInMillis)
                    Log.e("selectTime","calendarMin: "+calendarMin.timeInMillis)
                    if(dateByFormat.time<=calendarMax.timeInMillis && dateByFormat.time>=calendarMin.timeInMillis){
                        success(timeStr, Date())
                    }else{
                        Toast.makeText(activity,"所选时间超出范围，请联系管理员",Toast.LENGTH_SHORT).show()
                        return@setPositiveButton
                    }
                }else{
                    success(timeStr, Date())
                }
            } catch (e: Exception) {
                Toast.makeText(activity, "" + e.message, Toast.LENGTH_SHORT).show()
                return@setPositiveButton
            }
            dialog.dismiss()
        }.create().show()

    }


    /**
     * String类型的日期时间转化为Date类型
     *
     * @param strDate String形式的日期时间
     * @param format  格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return Date Date类型日期时间
     */
    private fun getDateByFormat(strDate: String?, format: String?): Date? {
        if (strDate == null) return null
        val mSimpleDateFormat = SimpleDateFormat(format, Locale.getDefault())
        var date: Date? = null
        try {
            date = mSimpleDateFormat.parse(strDate)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return date
    }
    private fun getMaxDay(yearValue: Int, monthValue: Int): Int {
        var maxDay = 28
        if (monthValue == 1 || monthValue == 3 || monthValue == 5 || monthValue == 7 || monthValue == 8 || monthValue == 10 || monthValue == 12) {
            maxDay = 31
        } else if (monthValue == 4 || monthValue == 6 || monthValue == 9 || monthValue == 11) {
            maxDay = 30
        } else if (monthValue == 2) {
            if (yearValue % 100 == 0) {
                if (yearValue % 400 == 0) maxDay = 29
            } else if (yearValue % 4 == 0) {
                maxDay = 29
            }
        }
        return maxDay
    }

    fun dateSelector(activity: Activity, defaultTimeStr: String? = null, showSplit: Boolean = false, success: (String, Date) -> Unit) {
        val binding = DialogDateSelectorYmdhmsSiegeLionBinding.inflate(LayoutInflater.from(activity))
        if (!showSplit) {
            binding.tvYearSplit.visibility = View.GONE
            binding.tvMonthSplit.visibility = View.GONE
            binding.tvDaySplit.visibility = View.GONE

            binding.tvHourSplit.visibility = View.GONE
            binding.tvMinuteSplit.visibility = View.GONE
            binding.tvSecondSplit.visibility = View.GONE
        }
        val calendar = Calendar.getInstance()
        try {
            if (null != defaultTimeStr) { //"yyyy/MM/dd HH:mm:ss"
                val mSimpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault())
                val date = mSimpleDateFormat.parse(defaultTimeStr)
                if (null != date) calendar.time = date
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        setUnableEdit(binding.npYear) //android:descendantFocusability="blocksDescendants"
        setUnableEdit(binding.npMonth)
        setUnableEdit(binding.npDay)
        setUnableEdit(binding.npHour)
        setUnableEdit(binding.npMinute)
        setUnableEdit(binding.npSecond)

        binding.npYear.minValue = 1970
        binding.npYear.maxValue = 2099
        binding.npYear.value = calendar[Calendar.YEAR]

        binding.npMonth.minValue = 1
        binding.npMonth.maxValue = 12
        binding.npMonth.value = calendar[Calendar.MONTH] + 1

        binding.npDay.minValue = 1
        binding.npDay.maxValue = 31
        binding.npDay.value = calendar[Calendar.DAY_OF_MONTH]

        binding.npHour.minValue = 0
        binding.npHour.maxValue = 23
        binding.npHour.value = calendar[Calendar.HOUR_OF_DAY]

        binding.npMinute.minValue = 0
        binding.npMinute.maxValue = 59
        binding.npMinute.value = calendar[Calendar.MINUTE]

        binding.npSecond.minValue = 0
        binding.npSecond.maxValue = 59
        binding.npSecond.value = calendar[Calendar.SECOND]

        binding.npYear.setOnValueChangedListener { picker, _, newVal ->
            val monthValue = binding.npMonth.value
            if (monthValue == 1 || monthValue == 3 || monthValue == 5 || monthValue == 7 || monthValue == 8 || monthValue == 10 || monthValue == 12) {
                binding.npDay.maxValue = 31
            } else if (monthValue == 4 || monthValue == 6 || monthValue == 9 || monthValue == 11) {
                binding.npDay.maxValue = 30
            } else if (monthValue == 2) {
                if (newVal % 100 == 0) {
                    if (newVal % 400 == 0) binding.npDay.maxValue = 29 else binding.npDay.maxValue = 28
                } else if (newVal % 4 == 0) {
                    binding.npDay.maxValue = 29
                } else {
                    binding.npDay.maxValue = 28
                }
            }
        }

        binding.npMonth.setOnValueChangedListener { picker, _, newVal ->
            val yearValue = binding.npYear.value
            if (newVal == 1 || newVal == 3 || newVal == 5 || newVal == 7 || newVal == 8 || newVal == 10 || newVal == 12) {
                binding.npDay.maxValue = 31
            } else if (newVal == 4 || newVal == 6 || newVal == 9 || newVal == 11) {
                binding.npDay.maxValue = 30
            } else if (newVal == 2) {
                if (yearValue % 100 == 0) {
                    if (yearValue % 400 == 0) binding.npDay.maxValue = 29 else binding.npDay.maxValue = 28
                } else if (yearValue % 4 == 0) {
                    binding.npDay.maxValue = 29
                } else {
                    binding.npDay.maxValue = 28
                }
            }
        }

        AlertDialog.Builder(activity).setTitle("时间选择").setView(binding.root).setNegativeButton("取消") { dialog, _ ->
            dialog.dismiss()
        }.setPositiveButton("确定") { dialog, _ ->
            try {
                val ymd = "" + binding.npYear.value + "/" + binding.npMonth.value + "/" + binding.npDay.value
                val hms = "" + binding.npHour.value + ":" + binding.npMinute.value + ":" + binding.npSecond.value
                val timeStr = "$ymd $hms"
                success(timeStr, Date())
            } catch (e: Exception) {
                Toast.makeText(activity, "" + e.message, Toast.LENGTH_SHORT).show()
                return@setPositiveButton
            }
            dialog.dismiss()
        }.create().show()
    }

    private fun setUnableEdit(npYear: NumberPicker) { //        for (i in 0 until npYear.childCount) {
        //            if (npYear.getChildAt(i) is EditText) {
        //                (npYear.getChildAt(i) as EditText).isEnabled = false
        //            }
        //        }
        npYear.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
    }

    fun dateChangeVerSelector(activity: Activity, defaultTimeStr: String? = null, showSplit: Boolean = false, success: (String, Date) -> Unit) {
        val splitDate = "/" //日期分隔符
        val splitTime = ":" //时间分隔符
        val binding = DialogDateSelectorYmdhmsChangeVerSiegeLionBinding.inflate(LayoutInflater.from(activity))
        if (!showSplit) {
            binding.tvYearSplit.visibility = View.GONE
            binding.tvMonthSplit.visibility = View.GONE
            binding.tvDaySplit.visibility = View.GONE

            binding.tvHourSplit.visibility = View.GONE
            binding.tvMinuteSplit.visibility = View.GONE
            binding.tvSecondSplit.visibility = View.GONE
        }
        val calendar = Calendar.getInstance()
        try {
            if (null != defaultTimeStr) { //"yyyy/MM/dd HH:mm:ss"
                val mSimpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault())
                val date = mSimpleDateFormat.parse(defaultTimeStr)
                if (null != date) calendar.time = date
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        setUnableEdit(binding.npYear)
        setUnableEdit(binding.npMonth)
        setUnableEdit(binding.npDay)
        setUnableEdit(binding.npHour)
        setUnableEdit(binding.npMinute)
        setUnableEdit(binding.npSecond)

        binding.npYear.minValue = 1970
        binding.npYear.maxValue = 2099
        binding.npYear.value = calendar[Calendar.YEAR]

        binding.npMonth.minValue = 1
        binding.npMonth.maxValue = 12
        binding.npMonth.value = calendar[Calendar.MONTH] + 1
        setDataFormatter(binding.npMonth)

        binding.npDay.minValue = 1
        binding.npDay.maxValue = 31
        binding.npDay.value = calendar[Calendar.DAY_OF_MONTH]
        setDataFormatter(binding.npDay)

        binding.npHour.minValue = 0
        binding.npHour.maxValue = 23
        binding.npHour.value = calendar[Calendar.HOUR_OF_DAY]
        setDataFormatter(binding.npHour)

        binding.npMinute.minValue = 0
        binding.npMinute.maxValue = 59
        binding.npMinute.value = calendar[Calendar.MINUTE]
        setDataFormatter(binding.npMinute)

        binding.npSecond.minValue = 0
        binding.npSecond.maxValue = 59

        binding.npSecond.value = calendar[Calendar.SECOND]
        setDataFormatter(binding.npSecond)

        binding.npYear.setOnValueChangedListener { picker, _, newVal ->
            val monthValue = binding.npMonth.value
            if (monthValue == 1 || monthValue == 3 || monthValue == 5 || monthValue == 7 || monthValue == 8 || monthValue == 10 || monthValue == 12) {
                binding.npDay.maxValue = 31
            } else if (monthValue == 4 || monthValue == 6 || monthValue == 9 || monthValue == 11) {
                binding.npDay.maxValue = 30
            } else if (monthValue == 2) {
                if (newVal % 100 == 0) {
                    if (newVal % 400 == 0) binding.npDay.maxValue = 29 else binding.npDay.maxValue = 28
                } else if (newVal % 4 == 0) {
                    binding.npDay.maxValue = 29
                } else {
                    binding.npDay.maxValue = 28
                }
            }
        }

        binding.npMonth.setOnValueChangedListener { _, _, newVal ->
            val yearValue = binding.npYear.value
            if (newVal == 1 || newVal == 3 || newVal == 5 || newVal == 7 || newVal == 8 || newVal == 10 || newVal == 12) {
                binding.npDay.maxValue = 31
            } else if (newVal == 4 || newVal == 6 || newVal == 9 || newVal == 11) {
                binding.npDay.maxValue = 30
            } else if (newVal == 2) {
                if (yearValue % 100 == 0) {
                    if (yearValue % 400 == 0) binding.npDay.maxValue = 29 else binding.npDay.maxValue = 28
                } else if (yearValue % 4 == 0) {
                    binding.npDay.maxValue = 29
                } else {
                    binding.npDay.maxValue = 28
                }
            }
        }

        val lineColorStr = "#FF0000"
        binding.npYear.setNumberPickerDividerColor(binding.npYear, lineColorStr)
        binding.npMonth.setNumberPickerDividerColor(binding.npMonth, lineColorStr)
        binding.npDay.setNumberPickerDividerColor(binding.npDay, lineColorStr)
        binding.npHour.setNumberPickerDividerColor(binding.npHour, lineColorStr)
        binding.npMinute.setNumberPickerDividerColor(binding.npMinute, lineColorStr)
        binding.npSecond.setNumberPickerDividerColor(binding.npSecond, lineColorStr)

        AlertDialog.Builder(activity).setTitle("时间选择").setView(binding.root).setNegativeButton("取消") { dialog, _ ->
            dialog.dismiss()
        }.setPositiveButton("确定") { dialog, _ ->
            try {
                val ymd = (getDataFormater(binding.npYear)) + //
                        splitDate + (getDataFormater(binding.npMonth)) + //
                        splitDate + (getDataFormater(binding.npDay)) //

                val hms = (getDataFormater(binding.npHour)) + //
                        splitTime + (getDataFormater(binding.npMinute)) + //
                        splitTime + (getDataFormater(binding.npSecond)) //
                val timeStr = "$ymd $hms"
                success(timeStr, Date())
            } catch (e: Exception) {
                Toast.makeText(activity, "" + e.message, Toast.LENGTH_SHORT).show()
                return@setPositiveButton
            }
            dialog.dismiss()
        }.create().show()
    }

    private fun getDataFormater(npYear: ChargeNumberPicker): String {
        return if (npYear.value < 10) "0" + npYear.value else "" + npYear.value
    }

    /**
     * 设置格式
     */
    private fun setDataFormatter(npMonth: ChargeNumberPicker) {
        npMonth.setFormatter {
            if (it < 10) "0$it" else "" + it
        }
    }

}
